/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tests.taskgraph.tasks.data;


public class Product implements Comparable<Product> {
	
	protected long id;
	protected String name;
	protected float price;

	/**
	 * Default constructor.
	 */
	public Product() {
		// do nothing
	}
	
	/**
	 * Product constructor.
	 * 
	 * @param id    The numeric key.
	 * @param name  The product name.
	 * @param price The product price.
	 */
	public Product(long id, String name, float price) {
		setId(id);
		setName(name);
		setPrice(price);
	}
	
	/**
	 * Copy constructor.
	 * 
	 * @param product The product to copy.
	 */
	public Product(Product product) {
		id = product.id;
		name = product.name;
		price = product.price;
	}
	
	@Override
	public int compareTo(Product p) {
		// Works as long as all id's are positive 
		return (int) (id - p.id);
	}

	@Override
    public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof Product)) {
			return false;
		}
		Product p = (Product) obj;
    	return (id == p.id);
    }
	
	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @return the price
	 */
	public float getPrice() {
		return price;
	}

	@Override
	public int hashCode() {
		// See Effective Java Item 9.
		int result = 51;
		result = 31 * result + (int) (id ^ (id >>> 32));
		result = 31 * result + Float.floatToIntBits(price);
		return result;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrice(float price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return String.format("%5d %15s $%f", id, name, price);
	}

}
